package com.weimob.mcloud.wechatwork.customer.convertor.tag;

import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.FollowUserBO;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.Customer;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Employee;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.Tag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagDetail;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagGroup;
import com.weimob.mcloud.wechatwork.customer.convertor.bos.PrivateSphereCovert;
import com.weimob.mcloud.wechatwork.customer.service.user.base.PrivateSphereDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.enums.tx.FollowUserTagTypeEnum;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.customer.TagDetailBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.tag.CustomerTagDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.tag.TagDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/12/22 8:18 下午
 */
public class CustomerTagCovert {

    public static List<CustomerTagDTO> toCustomerTagDTOS(List<TagDetail> tagDetailList) {
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return new ArrayList<>();
        }


        Map<String, List<TagDetail>> map = new HashMap<>();
        for (TagDetail tagDetail : tagDetailList) {

            Customer customer = tagDetail.getCustomer();
            Employee employee = tagDetail.getEmployee();
            if (Objects.isNull(customer) || StringUtils.isBlank(customer.getId())) {
                continue;
            }

            if (Objects.isNull(employee) || StringUtils.isBlank(employee.getId())) {
                continue;
            }
            String unionFlag = customer.getId() + employee.getId();

            List<TagDetail> tagDetails = map.get(unionFlag);
            if (CollectionUtils.isEmpty(tagDetails)) {
                map.put(unionFlag, Lists.newArrayList(tagDetail));
            } else {
                tagDetails.add(tagDetail);
            }

        }


        List<CustomerTagDTO> customerTagDTOS = new ArrayList<>();
        for (Map.Entry<String, List<TagDetail>> entry : map.entrySet()) {
            List<TagDetail> value = entry.getValue();

            if (CollectionUtils.isEmpty(value)) {
                continue;
            }

            CustomerTagDTO customerTagDTO = new CustomerTagDTO();
            TagDetail tagDetail = value.get(0);


            Customer customer = tagDetail.getCustomer();
            customerTagDTO.setCustomerId(customer.getId());
            customerTagDTO.setWid(customer.getWid());
            customerTagDTO.setExternalUserId(customer.getExternalUserid());

            Employee employee = tagDetail.getEmployee();
            customerTagDTO.setOrgUserId(employee.getId());
            customerTagDTO.setUserId(employee.getTxUserId());

            PrivateSphere privateSphere = tagDetail.getPrivateSphere();
            PrivateSphereDTO privateSphereDTO = PrivateSphereCovert.privateSphereDTO(privateSphere);
            customerTagDTO.setPrivateSphereDTO(privateSphereDTO);

            List<TagDTO> tagDTOList = new ArrayList<>();
            for (TagDetail tagDetailInfo : value) {
                Tag tag = tagDetailInfo.getTag();
                if (Objects.isNull(tag)) {
                    continue;
                }

                TagDTO tagDTO = TagConvert.toTagDTO(tag);
                if (Objects.nonNull(tagDTO)) {
                    tagDTOList.add(tagDTO);
                }
            }

            customerTagDTO.setTagDTOList(tagDTOList);

            customerTagDTOS.add(customerTagDTO);
        }

        return customerTagDTOS;
    }

    public static Map<String, List<TagDetail>> toOrgUserIdAndCustomerTagMap(List<TagDetail> request) {
        if (CollectionUtils.isEmpty(request)) {
            return new HashMap<>();
        }

        Map<String, List<TagDetail>> map = new HashMap<>();
        for (TagDetail tagDetail : request) {
            Employee employee = tagDetail.getEmployee();

            List<TagDetail> tagDetailList = map.get(employee.getId());
            if (CollectionUtils.isEmpty(tagDetailList)) {
                map.put(employee.getId(), Lists.newArrayList(tagDetail));
            } else {
                tagDetailList.add(tagDetail);
            }
        }

        return map;
    }

    public static Set<String> toCorpTagIdSet(List<TagDetail> tagDetailList) {
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return new HashSet<>();
        }

        Set<String> corpTagSet = new HashSet<>();
        for (TagDetail tagDetail : tagDetailList) {
            Tag tag = tagDetail.getTag();
            if (Objects.isNull(tag)) {
                continue;
            }

            corpTagSet.add(tag.getExternalTagId());
        }

        return corpTagSet;
    }

    public static List<TagDetailBO> toTagDetailBO(List<TagDetail> tagDetailList) {
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return new ArrayList<>();
        }

        List<TagDetailBO> tagDetailBOS = new ArrayList<>();
        for (TagDetail tagDetail : tagDetailList) {
            Tag tag = tagDetail.getTag();
            if (Objects.isNull(tag)) {
                continue;
            }
            TagDetailBO tagDetailBO = new TagDetailBO();
            tagDetailBO.setCorpTagId(tag.getExternalTagId());
            tagDetailBO.setTagName(tag.getTagName());
            tagDetailBO.setTagSource(tag.getTagSource());

            TagGroup tagGroup = tag.getTagGroup();
            if (Objects.nonNull(tagGroup)) {
                tagDetailBO.setCorpTagGroupId(tagGroup.getExternalTagGroupId());

                tagDetailBO.setTagGroupName(tagGroup.getTagGroupName());
            }

            tagDetailBOS.add(tagDetailBO);
        }

        return tagDetailBOS;
    }

    public static Set<String> toCorpTagGroupIdSet(List<TagDetail> tagDetailList) {
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return new HashSet<>();
        }

        Set<String> corpTagSet = new HashSet<>();
        for (TagDetail tagDetail : tagDetailList) {
            Tag tag = tagDetail.getTag();
            if (Objects.isNull(tag)) {
                continue;
            }

            TagGroup tagGroup = tag.getTagGroup();
            if (Objects.isNull(tagGroup)) {
                continue;
            }

            corpTagSet.add(tagGroup.getExternalTagGroupId());
        }

        return corpTagSet;
    }

    public static List<Tag> toTagList(List<TagDetail> tagDetailList) {
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return new ArrayList<>();
        }
        List<Tag> tagList = new ArrayList<>();
        for (TagDetail tagDetail : tagDetailList) {
            tagList.add(tagDetail.getTag());
        }

        return tagList;

    }

    public static Set<String> toCorpTagIdsFromFollowUser(List<FollowUserBO.TagBO> tags) {
        if (CollectionUtils.isEmpty(tags)) {
            return new HashSet<>();
        }

        Set<String> txCustomerTagIdsRequest = new HashSet<>();
        if (CollectionUtils.isNotEmpty(tags)) {
            for (FollowUserBO.TagBO txTagBO : tags) {
                //过滤腾讯过来的自定义标签,用户自定义类型标签（type=2）不返回
                if (FollowUserTagTypeEnum.CORP_SET.getType().equals(txTagBO.getType())) {
                    txCustomerTagIdsRequest.add(txTagBO.getTagId());
                }
            }
        }


        return txCustomerTagIdsRequest;
    }

    public static Set<String> toTagIdsFromTXUserList(List<FollowUserBO> followUsers) {
        if (CollectionUtils.isEmpty(followUsers)) {
            return new HashSet<>();
        }

        Set<String> tagIds = new HashSet<>();
        for (FollowUserBO followUserBO : followUsers) {
            Set<String> followUserTagIds = toCorpTagIdsFromFollowUser(followUserBO.getTags());
            if (CollectionUtils.isNotEmpty(followUserTagIds)) {
                tagIds.addAll(followUserTagIds);
            }
        }

        return tagIds;
    }

    public static List<String> toTagIdsFromAnalysisUserBO(List<FollowUserBO.TagBO> tags) {
        if (CollectionUtils.isEmpty(tags)) {
            return new ArrayList<>();
        }

        List<String> txCustomerTagIdsRequest = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(tags)) {
            for (FollowUserBO.TagBO txTagBO : tags) {
                //过滤腾讯过来的自定义标签,用户自定义类型标签（type=2）不返回
                if (FollowUserTagTypeEnum.CORP_SET.getType().equals(txTagBO.getType())) {
                    txCustomerTagIdsRequest.add(txTagBO.getTagId());
                }
            }
        }

        return txCustomerTagIdsRequest;
    }

    public static List<String> toTagIds(List<TagDetail> tagDetails) {
        if (CollectionUtils.isEmpty(tagDetails)) {
            return new ArrayList<>();
        }

        List<String> tagIdList = new ArrayList<>();
        for (TagDetail tagDetail : tagDetails) {
            Tag tag = tagDetail.getTag();
            if (null == tag) {
                continue;
            }
            tagIdList.add(tag.getExternalTagId());
        }

        return tagIdList;
    }

}
